from django.shortcuts import render

# Create your views here.
from drf_haystack.viewsets import HaystackViewSet

from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView

from goods.models import SKU
from goods.serializers import SKUSerializer, SKUIndexSerializer
from meiduo_mall.utils.pagination import StandardResultsSetPagination


class SKUListView(ListAPIView):
    """
    sku列表数据
    """
    serializer_class = SKUSerializer
    # 排序字段 按照什么进行排序
    pagination_class = StandardResultsSetPagination  # 分页功能
    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time', 'price', 'sales')

    # 这个方法查询不了 是因为没有id值返回
    # queryset = SKU.objects.filter()

    def get_queryset(self):
        category_id = self.kwargs['pk']
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
        SKU搜索
        """
    index_models = [SKU]
    pagination_class = StandardResultsSetPagination  # 分页功能
    serializer_class = SKUIndexSerializer

